﻿using Geek.Server.Core.Hotfix;
using NLog;

namespace Geek.Server.Core.Net.Tcp.Handler
{
    public abstract class BaseMessageHandler
    {
        static readonly Logger LOGGER = LogManager.GetCurrentClassLogger();
        
        public NetChannel Channel { get; set; }

        public Message Msg { get; set; }

        public virtual Task Init()
        {
            return Task.CompletedTask;
        }

        protected async Task SafeActionAsync()
        {
            try
            {
                await ActionAsync();
            }
            catch (Exception e)
            {
                LOGGER.Fatal(e);
                
                HotfixMgr.InvokeExceptionRpc(Channel, Msg);
            }
        }

        protected abstract Task ActionAsync();

        public virtual Task InnerAction()
        {
            return SafeActionAsync();
        }

    }
}
